# -*- coding: utf-8 -*-
# Project  : JXC
# FileName : RedisHandler.py
# Time     : 2020/7/31 10:59
# Author  : Mason101

import redis
from JXC.config import config


class RedisHandler:
    def __init__(self):
        self.conn = self.__connection()

    def __connection(self):
        redis_config = config["redis_config"]
        conn = redis.StrictRedis(host=redis_config["host"], port=redis_config["port"], db=redis_config["db"],
                                 decode_responses=True, socket_connect_timeout=3)
        return conn

    def get(self, key):
        return self.conn.get(key)


if __name__ == '__main__':
    conn = RedisHandler().conn
    ip = "127.0.0.1"
    # if conn.exists(ip):
    #     print(conn.ttl(ip))
    #     conn.rpush(ip, "2")
    # else:
    #     # conn.set(ip, 1, ex=60)     # 60s过期
    #     conn.rpush(ip, "1")
    #     conn.expire(ip, 60)         # 60s过期
    # print(conn.llen(ip))
    # conn.delete(ip)

    if conn.exists(ip):
        conn.setrange(ip, 0, int(conn.get(ip))+1)
    else:
        conn.set(ip, 1, ex=60)
    print(f"ip：{ip}，访问次数：{conn.get(ip)}，剩余有效期：{conn.ttl(ip)}s")
    print("over")
